define([], function() {
    'use strict';
    /**
     * 点击出现的提示弹窗
     * @param el - 触发的容器的选择器
     * @param content - 弹窗中的内容
     * @param display - 显示的display block, inline-block;
     * @param showClose - 显示关闭图标，默认为false
     * @param outClickHide - 点击外部关闭Popover，默认为false
     */
    function Popover(option){
        if(!option.el){
            throw new Error('id is missing');
        }
        this.el = option.el;
        this.box = $(option.el);
        this.content = option.content;
        this.display = option.display || 'inline-block';
        this.showClose = !option.showClose ? false : true;
        this.outClickHide = !option.outClickHide ? false : true;
        this.init();
    }

    Popover.prototype.init = function(){
        this.render();
        this.setListener();
    }

    Popover.prototype.render = function(){
        var that = this;
        this.box.wrap('<div class="yby-popover-box"></div>');
        this.box.parent('.yby-popover-box').css('display', this.display).append(
            '<div class="yby-popover-dialog">'
            + (!that.showClose ? '' : '<span class="popover-close">x</span>')
            + this.content
            + '</div>'
        );
    }

    Popover.prototype.showPopover = function(){
        this.box.parent().find('.yby-popover-dialog').show();
    }

    Popover.prototype.hidePopover = function(){
        this.box.parent().find('.yby-popover-dialog').hide();
    }

    Popover.prototype.getState = function(){
        var display = this.box.parent().find('.yby-popover-dialog').css('display');
        return display == "none" ? 'hide' : 'show';
    }

    Popover.prototype.setFunc = function(){
        var that = this;
        this.boxClick = function(e){
            if(that.getState() == 'hide'){
                that.showPopover();
            }
        }
        this.parentClick = function(e){
            e.preventDefault();
            e.stopPropagation();
        }
        this.closeClick = function(e){
            that.hidePopover();
        }
        this.outerClick = function(e){
            that.hidePopover();
        }
    }

    Popover.prototype.setListener = function(){
        var that = this;
        this.box.off('click', this.boxClick);
        this.box.on('click', this.boxClick);

        this.box.parent().off('click', this.parentClick);
        this.box.parent().on('click', this.parentClick);

        this.box.parent().off('click', '.popover-close', this.closeClick);
        this.box.parent().on('click', '.popover-close', this.closeClick);
        if(this.outClickHide){
            $(document).off('click', this.outerClick);
            $(document).on('click', this.outerClick);
        }
    }

    return Popover;
});